package com.alks.function.controller.finance.ryfinance;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.finance.ryfinance.MtlPreOtherInDto;
import com.alks.function.data.request.finance.ryfinance.MtlPreOtherInRequest;
import com.alks.function.data.request.finance.ryfinance.MtlPreOtherUpRequest;
import com.alks.function.mapper.finance.ryfinance.PcMtlPreOtherMapper;
import com.alks.function.service.finance.ryfinance.IRecTbAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiRestController("/ryFinance")
@Api(value = "财务部", tags = "财务部-荣阳财务-收货对账作业")
@Slf4j
public class RecTbAccountController {
    @Autowired
    private IRecTbAccountService service;

    @WebLogger
    @PostMapping("/mtlPreOtherInQuery")
    @ApiOperation(value = "入库明细查询-分页可条件查询", tags = "财务部-荣阳财务-收货对账作业")
    public ResponseInfo mtlPreOtherInQuery(@RequestBody MtlPreOtherInRequest request) {
        return service.mtlPreOtherInQuery(request);
    }

    @WebLogger
    @PutMapping("/mtlPreOtherInUp")
    @ApiOperation(value = "入库明细更新（包括结算）-更新", tags = "财务部-荣阳财务-收货对账作业")
    public ResponseInfo mtlPreOtherInUp(@RequestBody List<MtlPreOtherUpRequest> request) {
        return service.mtlPreOtherInUp(request);
    }

    @WebLogger
    @GetMapping("/mtlPreOtherInBack")
    @ApiOperation(value = "备注信息回显查询-回显", tags = "财务部-荣阳财务-收货对账作业")
    public ResponseInfo mtlPreOtherInBack() {
        return service.mtlPreOtherInBack();
    }

    @Autowired
    private PcMtlPreOtherMapper otherMapper;

    @PostMapping("/downloadMtlPreOther")
    @WebLogger
    @ApiOperation(value = "导出明细", tags = "财务部-荣阳财务-收货对账作业")
    public void  downloadCutYield(HttpServletResponse response, @RequestBody MtlPreOtherInRequest request) throws IOException {
        Map<String, Object> map = new HashMap<>();
        List<MtlPreOtherInDto> dtos = otherMapper.mtlPreOtherInQuery(request, UserIdThread.get().getCompanyId());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());
        int total = dtos.size();
        map.put("date",date);
        map.put("total",total);
        ExcelUtils.creatByTemplate(response,"收货对账作业模板.xls",date,dtos);

        //  service.downloadMtlPreOther(response,request);
    }


}
